package io.serializable;

import framework.AbstractTest;
import utils.Log;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by shane on 2018/5/28.
 */


abstract class Shape implements Serializable {
    public static final int RED = 1, BLUE = 2, GREEN = 3;

    private int xPos, yPos, dimension;

    private static Random rand = new Random(47);
    private static int counter = 0;

    public abstract void setColor(int newColor);
    public abstract int getColor();

    public Shape(int x, int y, int d) {
        xPos = x;
        yPos = y;
        dimension = d;
    }

    @Override
    public String toString() {
        return super.getClass() + "color[" + getColor() + "] xPos[" + xPos + "] yPos[" + yPos + "] dimension[" + dimension + "]\n";
    }


    public static Shape randomFactory() {
        int x = rand.nextInt(100);
        int y = rand.nextInt(100);
        int d = rand.nextInt(100);

        switch (counter++ % 3) {
            default:
            case 0:return new Circle(x,y,d);
            case 1:return new Square(x,y,d);
            case 2:return new Line(x,y,d);
        }
    }

}

class Circle extends Shape {
    private static int color = RED;

    public Circle(int x, int y, int d) {
        super(x,y,d);
    }

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }

    @Override
    public int getColor() {
        return color;
    }
}


class Square extends Shape {

    private static int color;

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }

    @Override
    public int getColor() {
        return color;
    }

    public Square(int x, int y, int d) {
        super(x, y, d);
        color = RED;
    }
}

class Line extends Shape {
    private static int color = RED;

    @Override
    public void setColor(int newColor) {
        color = newColor;
    }

    @Override
    public int getColor() {
        return color;
    }

    public Line(int x, int y, int d) {
        super(x, y, d);
    }

    public static void serializeStaticState(ObjectOutputStream os) throws IOException {
        os.writeInt(color);
    }

    public static void deserializeStaticState(ObjectInputStream os) throws IOException {
        color = os.readInt();
    }
}


public class StoreCADState extends AbstractTest {
    @Override
    public void test(String[] args) {
        super.test(args);

        try {
            List<Class<? extends Shape>> shapes = new ArrayList<>();
            // add
            shapes.add(Circle.class);
            shapes.add(Square.class);
            shapes.add(Line.class);

            List<Shape> s = new ArrayList<>();
            // MAKE
            for (int i = 0; i < 10;i++) {
                s.add(Shape.randomFactory());
            }
            // set
            for (int i = 0; i < 10;i++) {
                ((Shape)s.get(i)).setColor(Shape.GREEN);
            }

            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(getDefTempFile(args)));
            out.writeObject(shapes);
            Line.serializeStaticState(out);
            out.writeObject(s);

            //
            Log.vn(s.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
